<?php

session_start();

require_once '../controllers/admin.guilds.class.php';
require_once '../engine/view.class.php';

include('../inc/config.inc.php');
include('../inc/template.inc.php');
include('../inc/db.inc.php');
include('../inc/check.inc.php');

class AdminGuildsRanks extends AdminGuild
{
    public $listRanks = '';
    
    public function __construct() {
        parent::__construct();
    }
    
    public function listGuildRanks($id)
    {
        if($r = $this -> query("SELECT * FROM guildranks WHERE guild_id = $id ORDER BY rank_position DESC"))
        {
            while($rd = $r -> fetch_assoc()) 
            {
                
            }
        }
    }
}

if($_SESSION['admin'] == 1 OR $_SESSION['founder'] == 1) 
{
    $AdminGuildsRanks = new AdminGuildsRanks;
    
    $progress = ( ! empty($_GET['progress'])) ? $_GET['progress'] : 0;

    switch($progress) {
        case 0:	
	
            $AdminGuildsRanks -> optionGuilds();
        
            $tags = array(
                'game-name' => $AdminGuildsRanks::__GNAME,
                'options-guilds' => $AdminGuildsRanks -> options_guilds
            );

            $view = new view('style/A_guildranks.template.html', $tags);
        break;
        case 1:
            $list = '';
            
            if( ! empty($_GET['gid'])) 
            {
                $guild_id = $_GET['gid'];
            } else {
                $guild_id = $_POST['guildname'];
            }
            // Recupera l'elenco dei ranghi di gilda
            $query = "SELECT * FROM guildranks WHERE guild_id = '$guild_id' ORDER BY rank_position DESC";
                if($results = $db->query($query)) {
                    while($row = $results->fetch_assoc()) {
                        $rankedit = '<a href="A_guildranks-edit.php?id='.$row['rank_id'].'&gid='.$guild_id.'" target="main"  title="modifica">';
		if($row['rank_grade'] != 1){ // Se il rango non � quello di master, mostra i controlli per cancellarlo o spostarlo
			$rankdelete = "<a href='A_guildranks-execute.php?progress=1&gid=$guild_id&act=1&id=".$row['rank_id']."' target='main'  title='cancella'>X</a>";
			if ($row['rank_position'] + 1 <= 10){
			$rankmoveup	= "<a href='A_guildranks-execute.php?progress=1&gid=$guild_id&act=2&id=".$row['rank_id']."' target='main'  title='muovi su'>+</a>/";
			}else{$rankmoveup = "+"."/";}
			if ($row['rank_position'] - 1 >= 1){
			$rankmovedown = "<a href='A_guildranks-execute.php?progress=1&gid=$guild_id&act=3&id=".$row['rank_id']."' target='main'  title='muovi gi�'>-</a>";
			}else{$rankmovedown = "-";}
			$name = $row['rank_name'];
			}else{
			$rankdelete = "<strong>X"; 
			$rankmoveup = "+/"; 
			$rankmovedown = "-</strong>";
			$name = "<span class='tDefault'>".$row['rank_name']."</span>";
			}
			$list .= "<div>".$rankdelete." - ".$rankmoveup.$rankmovedown."  |  ".$row['rank_position']." ".$rankedit."<img src='../img/guilds/".$row['rank_image']."' width='20px' height='20px' border='0'> ".$name."</a></div>";
		
		}
		$results->close();
	}
	
// Se il rango � stato creato, lo stampa.
if($_GET['mod'] == 1){
	$msg_box .= '<div class="box_info">';
	$msg_box .= 'Rango creato con successo.';
	$msg_box .= '</div>';
}
// Se il rango � stato eliminato, lo stampa.
if($_GET['mod'] == 2){
	$msg_box .= '<div class="box_info">';
	$msg_box .= 'Rango eliminato con successo.';
	$msg_box .= '</div>';
}
// Se il rango � stato modificato, lo stampa.
if($_GET['mod'] == 3){
	$msg_box .= '<div class="box_info">';
	$msg_box .= 'Rango modificato con successo.';
	$msg_box .= '</div>';
}
// Lista delle tag speciali
	$tags = array('temp_title' => $g_name, 'temp_guildid' => $guild_id, 'temp_list' => $list, 'temp_msgbox' => $msg_box);

// Carica il template e formatta le tag speciali
	$temp = new template('style/A_guildranks2.template.html');
	$temp->replace($tags);
break;
}


	$view -> show();
	
} else {

	// Lista delle tag speciali
	$tags = array('temp_title' => $g_name, 'temp_error' => 'Accesso negato. Non si dispone di privilegi sufficienti.');

	// Carica il template e formatta le tag speciali
	$temp = new template('style/error.template.html');
	$temp->replace($tags);

	// Visualizza l'output
	echo $temp->show();
}

// Chiude la connessione al database
$db->close();

?>